﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

using System.Collections;
using System.Runtime.InteropServices;
using System.IO;

// namespace of imported WIA Scripting COM component
using WIA;

namespace ResUma
{
    public partial class ScanariFrm : FrmCard
    {
 
        public ScanariFrm(ModuleClass mc):base(mc)
        {
            InitializeComponent();           
        }

        private void ScanariFrm_Load(object sender, EventArgs e)
        {
            AddButton("Sursa", global::ERPStudio.Properties.Resources._001_39, Sursa, ToolStripItemAlignment.Right, true);
            AddButton("Scaneaza", global::ERPStudio.Properties.Resources._001_53, Scaneaza, ToolStripItemAlignment.Right, true);
 
        }

        private void Sursa(object sender, EventArgs e)
        {
        }

        private const int WIA_DPS_DOCUMENT_HANDLING_SELECT_FEEDER = 1;
        private const int WIA_DPS_DOCUMENT_HANDLING_SELECT_FLATBED = 2;
        private const int WIA_IPS_PAGES = 3096;


        private const int WIA_DPS_DOCUMENT_HANDLING_STATUS_FEED_READY= 1;


        private const int WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS =1024;
        private const int WIA_PROPERTIES_WIA_DIP_FIRST = 2;
        private const int WIA_PROPERTIES_WIA_DPA_FIRST = WIA_PROPERTIES_WIA_DIP_FIRST +  WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;
        private const int WIA_PROPERTIES_WIA_DPC_FIRST = WIA_PROPERTIES_WIA_DPA_FIRST +  WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;


        private const int WIA_PROPERTIES_WIA_DPS_FIRST = WIA_PROPERTIES_WIA_DPC_FIRST +  WIA_PROPERTIES_WIA_RESERVED_FOR_NEW_PROPS;
        private const int
        WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_PROPERTIES_WIA_DPS_FIRST + 13;
        private const int
        WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_PROPERTIES_WIA_DPS_FIRST + 14;
        private const string WIA_DPS_DOCUMENT_HANDLING_STATUS_STR = "Document Handling Status";


        private const double WIA_ERRORS_BASE_VAL_WIA_ERROR = 2149646336;
        private const double WIA_ERRORS_WIA_ERROR_PAPER_EMPTY = WIA_ERRORS_BASE_VAL_WIA_ERROR + 3;


        private const int WIA_IPS_CUR_INTENT = 6146;
        private const int WIA_IPS_XRES = 6147;
        private const int WIA_IPS_YRES = 6148;


        private const int WIA_INTENT_NONE = 0x00000000;
        private const int WIA_INTENT_IMAGE_TYPE_COLOR = 0x00000001;
        private const int WIA_INTENT_IMAGE_TYPE_GRAYSCALE = 0x00000002;
        private const int WIA_INTENT_IMAGE_TYPE_TEXT = 0x00000004;
        private const int WIA_INTENT_IMAGE_TYPE_MASK = 0x0000000F;
        private const int WIA_INTENT_MINIMIZE_SIZE = 0x00010000;
        private const int WIA_INTENT_MAXIMIZE_QUALITY = 0x00020000;
        private const int WIA_INTENT_BEST_PREVIEW = 0x00040000;
        private const int WIA_INTENT_SIZE_MASK = 0x000F0000;
        private const int FEED_READY = 0x01;


        private const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
        public const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";

        private void Scaneaza(object sender, EventArgs e)
        {
            /*WIA.CommonDialog dlg = new WIA.CommonDialog();

            Device dvc =dlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType,true,false);


            if (dvc != null)
            {
                dlg.ShowAcquisitionWizard(dvc);
            }
            */
            WIA.CommonDialog dialog = new WIA.CommonDialog();
            WIA.ImageFile file = dialog.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.UnspecifiedIntent,
                WIA.WiaImageBias.MaximizeQuality, wiaFormatBMP, true, true, false);
            WIA.Vector vector = file.FileData;
            byte[] bin = (byte[])vector.get_BinaryData();
            using (MemoryStream mstrm = new MemoryStream(bin))
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromStream(mstrm);
            }   

            //file.SaveFile(@"c:\myimage." + file.FileExtension);


            /*DeviceManager manager = new DeviceManager();
            object index = 1;


            DeviceInfo info = manager.DeviceInfos.get_Item(ref index);
            Device dvc = info.Connect();
            object res = 150;
            object intent = WIA_INTENT_IMAGE_TYPE_COLOR;
            object statusProp = WIA_DPS_DOCUMENT_HANDLING_STATUS_STR;
            int documentStatus = 0;
            object pgs = 0;
            object handler = WIA_DPS_DOCUMENT_HANDLING_SELECT_FEEDER;


            foreach (Property p in dvc.Properties)
            {
                switch (p.PropertyID)
                {
                    case
                    WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_SELECT:
                        p.set_Value(ref handler);
                        break;


                    case WIA_IPS_XRES:
                    case WIA_IPS_YRES:
                        p.set_Value(ref res);
                        break;


                    case WIA_IPS_CUR_INTENT:
                        p.set_Value(ref intent);
                        break;


                    case
                    WIA_PROPERTIES_WIA_DPS_DOCUMENT_HANDLING_STATUS:
                        documentStatus = (int)p.get_Value();
                        break;


                    case WIA_IPS_PAGES:
                        p.set_Value(ref pgs);
                        break;
                }
            }


            foreach (Item itm in dvc.Items)
            {
                while ((documentStatus & FEED_READY) == FEED_READY)
                {
                    itm.Transfer(wiaFormatPNG);
                    documentStatus = (int)dvc.Properties.get_Item(ref statusProp).get_Value();
                }
            }

            */
            /*FrmMainMdi mdi = (FrmMainMdi)MdiParent;
            ScanTwain();*/
        }

   

    }
}
